﻿using DbContext;
using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Model.DbContext;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SmartPlant.Services
{
    public class DrawingService : BaseService, IDrawingService
    {
        public async Task<List<Drawing>> GetPIDAsync(decimal ProjId, decimal plantId)
        {
            var sqlwhere = $"DWG_ID>0 AND DWG_TYPE_ID='1' and plant_id={plantId}";
            var select = "DWG_ID DWGID,DWG_NAME DWGNAME";
            var orderby = "DWG_NAME";
            string url = $"/api/Project/{ProjId}/drawing-api?sqlwhere={sqlwhere}&select={select}&orderby={orderby}";
            var res = await GetAsync<List<Drawing>>(url);
            return res.data;
        }

        public async Task<bool> UpdateDrawingAsync(decimal proId,Drawing drawing)
        {
            string uri = $"/api/Project/{proId}/drawing-api";
            var r = await PutBodyAsync<bool, Drawing>(uri, drawing);
            return r.data;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Drawing> GetById( decimal id)
        {
            string url = $"/api/Project/{GlobalObject.CurrentProject.ProjId}/drawing-api/{id}";
            var res = await GetAsync<Drawing>(url);
            return res.data;
        }
        public async Task<bool?> UpdateDwgs(decimal ProjId, List<Drawing> dwgs)
        {
            string url = $"/api/Project/{ProjId}/drawing-api/batch";
            var res = await PutBodyAsync<bool?, List<Drawing>>(url, dwgs);
            return res.data;
        }
        /// <summary>
        /// 创建pd，但是如果没有pd记录本身，还是会同时新建pd本身的
        /// </summary>
        /// <param name="dwgName"></param>
        /// <param name="Id"></param>
        /// <param name="type"></param>
        /// <param name="caseId"></param>
        /// <returns></returns>
        public async Task<bool> CreatePD(string dwgName, decimal Id, ObjectDefId type, decimal caseId)
        {
            string url = $"/api/Project/{GlobalObject.CurrentProject.ProjId}/drawing-api/pd-sheet/{dwgName}/{Id}/{type}/{caseId}";
            var res = await PostAsync<bool>(url);
            return res.data;
        }

        public async Task<bool> CreateSpec(string dwgName,int formId, decimal Id, ObjectDefId type, decimal caseId)
        {
            if (formId == 0 )
            {
                return false;
            }
            string url = $"/api/Project/{GlobalObject.CurrentProject.ProjId}/drawing-api/spec-sheet/{dwgName}/{formId}/{Id}/{type}/{caseId}";
            var res = await PostAsync<bool>(url);
            return res.data;
        }
    }
}
